<?php
namespace app\hotback\controller;
use app\service\UserWeixinService;
use app\service\TechnWeixinService;
use think\Db;
class User extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [];
				
				
				if(!empty($params['nickname']))
				{
					$map['nickname'] = ['like','%'.$params['nickname'].'%'];
				}
				
				if(!empty($params['mobile']))
				{
					$map['mobile'] = $params['mobile'];
				}
                if(!empty($params['city']))
                {
                    $shi = Db::name('proxy')->where('proxy_id',$params['city'])->find();
                    if ($shi['is_xian'] == 1){
                        $maps['county'] = $shi['qu'];
                    }else{
                        $not = Db::name('proxy')->where('shi',$shi['shi'])->where('proxy_id','<>',$shi['proxy_id'])->where('is_xian',1)->column('qu');
                        if (!empty($not)){
                            $not = implode(',',$not);
                            $maps['city'] = $shi['shi'];
                            $maps['county'] = ['not in',$not];
                        }else{
                            $maps['city'] = $shi['shi'];
                        }

                    }
                }
                $maps['id'] = ['>',0];
				// print_r($map);exit;
				
				$count = Db::name('user')->where($map)->where('is_qu',1)->count();
				$list_data = Db::name('user')->where($map)->where('is_qu',1)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
                        $city = Db::name('user_address')->where($maps)->where('user_id',$value['id'])->where('is_default',1)->find();
                        if(empty($city)){
                            $city = Db::name('user_address')->where($maps)->where('user_id',$value['id'])->find();
                        }
                        if (!empty($city)){
                            $list_data[$key]['address'] = $city['city_name'].$city['county_name'];
                        }
						$list_data[$key]['save_url'] = url('user/saveinfo', ['id' => $value['id']]);
						if(!empty($value['beizhu'])){
						    $list_data[$key]['nickname']=$value['nickname'].'('.$value['beizhu'].')';
						}
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
            $list = Db::name('proxy')->where('is_delete_time',0)->select();
            $this->assign('city',$list);
			return $this->fetch();
		}
	}

	/**
	 * 服添添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		
			//查询
		$info = Db::name('user')->where(['id' => $params['id']])->find();
			
		$this->assign('info',$info);
		
		return $this->fetch();
	}

	/**
	 * 服务添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			$params['upd_time'] = time();
			
			
			Db::name('user')->where(['id'=>intval($params['id'])])->update($params);
			//是否成功
			$result['code'] = 0;
			
			$result['msg'] = "操作成功";
			
		}
		echo json_encode($result);
	}

	/**
	 * 服务删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			
			if(Db::name('user')->where(['id' => intval($params['id'])])->delete())
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
	
	
	
	//推送测试
	public function tscs(){
	    
	    $params = input();
	    
		        $openid = Db::name('user_weixin')->where(['user_id' => $params['id'],'identity_type'=>'user'])->value('openid');
		        if(!empty($openid)){
		        $tem_msg['touser'] = $openid;
		        $tem_msg['template_id'] = 'uhmQUK0MNOSOTHFHmf66Z6OR0piSyTwXNmLYkJl4dBs';
		        
		        $tem_msg['data'] = [
		            'first' => [
		                'value' => '您好，有客户下单成功',
		                'color' => '#173177'
		            ],
		            'keyword1' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ],
		            'keyword2' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ],
		            'keyword3' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ],
		            'keyword4' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ],
		            'keyword5' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ],
		            'remark' => [
		                'value' => '测试',
		                'color' => '#173177'
		            ]
		        ];
				$a=UserWeixinService::send_wx_web_tem_msg($tem_msg);
				
				$result['code'] = 0;
				$result['msg'] = "成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "失败,微信没有授权";
			}
			
		echo json_encode($result);
			
	}
	
	
	
	public function fk()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{

                $map = [];
                

                $count = Db::name('user_fk')
                    ->where($map)
                    ->count();


                $list_data = Db::name('user_fk')
                    ->where($map)
                    ->limit(($params['page']-1)*$params['limit'],$params['limit'])
                    ->select();
                    
                if(!empty($list_data))
				{
				    
					foreach($list_data as $key => $value)
					{
					    
					    
                        $list_data[$key]['add_time'] = date("Y-m-d H:i:s",$value['add_time']);
                        
                        $list_data[$key]['nickname']=db::name('user')->where('id',$value['user_id'])->value('nickname');
                        
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}
	
	
	public function rechange_list(){
	    
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{

                $map = [];
                
                $map['status']=1;

                $count = Db::name('user_rechange')
                    ->where($map)
                    ->count();


                $list_data = Db::name('user_rechange')
                    ->where($map)
                    ->order('id desc')
                    ->limit(($params['page']-1)*$params['limit'],$params['limit'])
                    ->select();
                    
                if(!empty($list_data))
				{
				    
					foreach($list_data as $key => $value)
					{
					    
					    
                        $list_data[$key]['add_time'] = date("Y-m-d H:i:s",$value['add_time']);
                        
                        $list_data[$key]['nickname']=db::name('user')->where('id',$value['user_id'])->value('nickname');
                        
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

}